# -*- coding:utf-8 -*-

from feature_data.Dict2Org_Graphfeature import Feature_org_extration
from nd_service.second_stage_graph import Graph_org_stage
from answer_extration import Answer_extration
from nd_utils.networkx_util import *
from coorg_cluster import CoorgCluster

class Feature_venue_extration():
    def __init__(self, xml2dict):
        self.xml2dict = xml2dict

        self.publication_dict = self.xml2dict.publication_dict
        self.paperid_idx_dict = self.xml2dict.paperid_idx_dict

        self.paperidx_venue_dict = self._get_paperidx_venue_dict()

        self.second_stage_clust = self._get_second_stage_clust()
        self.init_graph = self._get_cluster_graph()

        self.second_subgraph_nodes_list = self.second_stage_clust.previous_subgraph_nodes_list

        self.covenueCluster_list = self._get_covenueCluster_list()
        self.cluster_covenue_pair_set_list = self._get_cluster_covenue_pair_set_list()

    """
        idx : title
    """
    def _get_paperidx_venue_dict(self):
        paper_venue_dict = {}
        for paperid, publication_attr_dict in self.publication_dict.items():
            venue =  publication_attr_dict['jconf']
            paper_venue_dict[ self.paperid_idx_dict[paperid] ] = venue
        return paper_venue_dict


    def _get_second_stage_clust(self):
        feature_org_extration = Feature_org_extration(self.xml2dict)
        answer_extration = Answer_extration(self.xml2dict)
        second_stage_clust = Graph_org_stage(feature_org_extration, answer_extration)
        return second_stage_clust

    def _get_cluster_graph(self):
        flag = self.second_stage_clust.simulation_occur_flag
        if flag:
            return self.second_stage_clust.second_cluster_graph
        else:
            return self.second_stage_clust.init_graph
    """
        封装已经 得到的 子图节点列表，
        提取新特征
    """
    def _get_covenueCluster_list(self):
        covenueCluster_list = []
        for paperidx_list in self.second_subgraph_nodes_list:
            covenueCluster = CoorgCluster(self.paperidx_venue_dict, paperidx_list)
            covenueCluster_list.append( covenueCluster )
        return covenueCluster_list
    """
        使用新特征，
        对 已经 得到的 子图 节点列表
        进行连接
    """
    def _get_cluster_covenue_pair_set_list(self):
        cluster_covenue_pair_set_list = []
        for i in range( len(self.covenueCluster_list) -1 ):
            for j in range(i+1, len(self.covenueCluster_list) ):
                venue_set1 = self.covenueCluster_list[i].cluster_venue_set
                venue_set2 = self.covenueCluster_list[j].cluster_venue_set
                covenue_set =  venue_set1.intersection(venue_set2)
                if len(covenue_set) != 0:
                    venue = list(covenue_set)[0]
                    idx1 = self.covenueCluster_list[i].get_cluster_idx_by_venue(venue)
                    idx2 = self.covenueCluster_list[j].get_cluster_idx_by_venue(venue)
                    cluster_covenue_pair_set_list.append( set([idx1, idx2]) )
        return cluster_covenue_pair_set_list


